﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Linq;
using System.IO;
using System.Diagnostics;
using System.Reflection;

namespace LogYuMin
{
    public class LogWriter
    {
        private DateTime LogTime
        {
            get;
            set;
        }

        private TextWriter SW
        {
            get;
            set;
        }

        private static Dictionary<string ,LogWriter> dicWriter=new Dictionary<string,LogWriter>();

        public static LogWriter GetWriter(LogFile key)
        {
            try
            {
              
                if (dicWriter.Keys.Contains(key.ToString()))
                {
                    LogWriter lw = dicWriter[key.ToString()];
                    if (DateTime.Now.Date == lw.LogTime.Date)
                    {
                        return lw;
                    }
                    else
                    {
                        lw.SW.Flush();
                        lw.SW.Close();
                        string logPath = ConfigurationManager.AppSettings["logPath"];
                        if (string.IsNullOrEmpty(logPath))
                        {
                            logPath = new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName+@"\";
                        }
                        string targetPath = key.ToString();
                        string fullPath =  logPath + "/" + targetPath + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
                        if (!File.Exists(fullPath))
                        {
                            lw.SW = File.CreateText(fullPath);
                        }
                        else
                        {
                            lw.SW = new StreamWriter(fullPath,true);
                        }
                        lw.LogTime = DateTime.Now.Date;
                    }
                    return lw;
                }
                else
                {
                    LogWriter lw = new LogWriter();
                    lw.LogTime = DateTime.Now.Date;
                    string logPath = ConfigurationManager.AppSettings["logPath"];
                    if (string.IsNullOrEmpty(logPath))
                    {
                        logPath = new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName + @"\";

                    }
                 
                    string targetPath = key.ToString();
                    string fullPath =logPath+"/" + targetPath + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
                  


                    if (!File.Exists(fullPath))
                    {
                        if (!Directory.Exists( logPath + "/" + targetPath + "/"))
                        {
                            Directory.CreateDirectory( logPath + "/" + targetPath + "/");
                        }
                        lw.SW = File.CreateText(fullPath);
                    }
                    else
                    {
                        lw.SW = new StreamWriter(fullPath,true);
                    }
                    dicWriter.Add(key.ToString(), lw);
                    return lw;
                }
            }
            catch
            {
                return new LogWriter();
            }

        }


        private static String LogTemplate = LogYuMin.Properties.Resources.log;
        

        public void Write( Type type, string method, LogStates state, string content)
        {
            try
            {
                if (LogTime == DateTime.MinValue)
                {
                    return;
                }
                string msg = LogTemplate;
                msg = msg.Replace("#Date#", DateTime.Now.ToString());
                msg = msg.Replace("#class#", type.FullName);
                msg = msg.Replace("#method#", method);
                msg = msg.Replace("#state#", state.ToString());
                msg = msg.Replace("#content#", content);
                
                SW.Write(msg);
                SW.Flush();
            }
            catch
            {
            }
        }

        public void Write(Type type, string method, LogStates state, Exception ex)
        {
            Write(type, method, state, ex.Message);
        }


        public void Write(string content)
        {
            try
            {
                if (LogTime == DateTime.MinValue)
                {
                    return;
                }
                string msg = LogYuMin.Properties.Resources.message;
                msg = msg.Replace("#Date#", DateTime.Now.ToString());
                msg = msg.Replace("#content#", content);
                SW.Write(msg);
                SW.Flush();
            }
            catch
            {
            }
        }
    }
}
